package inAction;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.LogMergePolicy;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

//page 47
public class Indexer {
	public static void main(String[] args) throws Exception {
	File indexDir = new File("index\\indexed");
	File dataDir = new File("index\\toBeIndexed");
	int numIndexed = index(indexDir, dataDir);
	}
	// open an index and start file directory traversal
	public static int index(File indexDir, File dataDir) throws IOException {
        FSDirectory dir = FSDirectory.open(new File("index\\toBeIndexed"));
    	StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_34);
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_34, analyzer);
        IndexWriter  writer = new IndexWriter(dir, config);
        writer.setUseCompoundFile(false);
		indexDirectory(writer, dataDir);
		int numIndexed = writer.maxDoc();
		writer.optimize();
		writer.close();
		return numIndexed;
	}

	// recursive method that calls itself when it finds a directory
	private static void indexDirectory(IndexWriter writer, File dir)
			throws IOException {
		File[] files = dir.listFiles();
		for (int i = 0; i < files.length; i++) {
			File f = files[i];
			if (f.isDirectory()) {
				indexDirectory(writer, f);
			} else if (f.getName().endsWith(".txt")) {
				indexFile(writer, f);
//				indexFileLine(writer, f);
			}
		}
	}

	// method to actually index a file using Lucene
	private static void indexFile(IndexWriter writer, File f)
			throws IOException {
		Document doc = new Document();
		doc.add(new Field("contents", new FileReader(f)));
		doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES,
				Field.Index.NOT_ANALYZED));
		writer.addDocument(doc);
	}
	private static void indexFileLine(IndexWriter writer, File f)
			throws IOException {
		Document doc = new Document();
		BufferedReader br=new BufferedReader(new FileReader(f));
		String s;
		while((s=br.readLine())!=null){
			String id=s.split(" ")[0];
			String value=s.split(" ")[1];
			doc.add(new Field("id",id, Field.Store.YES,Field.Index.NOT_ANALYZED));
			doc.add(new Field("value",value, Field.Store.YES,Field.Index.NOT_ANALYZED));
			writer.addDocument(doc);
		}
		// doc.add(new Field("value", new FileReader(f)));
	}
}